package consts

import (
	"github.com/gogf/gf/v2/errors/gcode"
)

// 20000 ~ 29999
var (
	ListFailed                        = gcode.New(20001, "查询列表失败", "")
	ListComponentFailed               = gcode.New(200011, "查询组件列表失败", "")
	ListPermissionFailed              = gcode.New(200012, "查询权限列表失败", "")
	ListRegionFailed                  = gcode.New(200013, "查询行政区域列表失败", "")
	ListRoleFailed                    = gcode.New(200019, "查询角色列表失败", "")
	ListTenantFailed                  = gcode.New(200024, "查询租户列表失败", "")
	ListUserFailed                    = gcode.New(200026, "查询用户失败", "")
	QueryAppFailed                    = gcode.New(200014, "查询应用失败", "")
	QueryAppPermissionFailed          = gcode.New(200015, "查询应用权限失败", "")
	QueryAppComponentFailed           = gcode.New(200016, "查询应用组件失败", "")
	QueryPolicyFailed                 = gcode.New(200017, "查询casbin策略失败", "")
	QueryRegionChildrenFailed         = gcode.New(200018, "查询区域的子区域失败", "")
	QueryRoleFailed                   = gcode.New(200020, "查询角色失败", "")
	QueryRolePolicyFailed             = gcode.New(200021, "查询角色关联policy失败", "")
	QueryGroupPolicyFailed            = gcode.New(200022, "查询组策略失败", "")
	QueryUserFailed                   = gcode.New(200023, "查询用户失败", "")
	QueryTenantFailed                 = gcode.New(200025, "查询租户失败", "")
	QueryUserGroupUserFailed          = gcode.New(200027, "查询用户组中的用户失败", "")
	QueryUserGroupFailed              = gcode.New(200028, "查询用户组失败", "")
	QueryOrganizationFailed           = gcode.New(200029, "查询组织失败", "")
	QueryAppMenuFailed                = gcode.New(200030, "查询App菜单失败", "")
	InfoFailed                        = gcode.New(20011, "获取详情失败", "")
	InfoAppComponentFailed            = gcode.New(200111, "获取应用组件详情失败", "")
	InfoAppMenuFailed                 = gcode.New(200112, "获取应用权限及菜单详情失败", "")
	GetCacheFailed                    = gcode.New(20002, "查询缓存失败", "")
	GetCachedPermissionsFailed        = gcode.New(200021, "查询权限缓存失败", "")
	ParseCacheFailed                  = gcode.New(20003, "缓存解析失败", "")
	ParsePermissionCacheFailed        = gcode.New(200031, "解析权限缓存失败", "")
	RegionCodeNotExist                = gcode.New(20004, "地区编码不存在", "")
	AddFailed                         = gcode.New(20008, "新增数据失败", "")
	AddComponentFailed                = gcode.New(200081, "新增组件失败", "")
	AddPermissionFailed               = gcode.New(200082, "新增权限失败", "")
	AddRoleFailed                     = gcode.New(200083, "新增角色失败", "")
	AddGroupPolicyFailed              = gcode.New(200084, "新增组策略失败", "")
	AddTenantFailed                   = gcode.New(200085, "新增租户失败", "")
	AddUserFailed                     = gcode.New(200086, "新增租户失败", "")
	EncryptFailed                     = gcode.New(20007, "加密失败", "")
	DeleteFailed                      = gcode.New(20009, "删除失败", "")
	DeleteAppFailed                   = gcode.New(200091, "删除应用失败", "")
	DeleteComponentFailed             = gcode.New(200092, "删除组件失败", "")
	DeletePermissionFailed            = gcode.New(200093, "删除权限失败", "")
	DeleteRegionFailed                = gcode.New(200094, "删除区域失败", "")
	DeleteRoleFailed                  = gcode.New(200095, "删除角色失败", "")
	DeleteTenantFailed                = gcode.New(200096, "删除租户失败", "")
	UpdateFailed                      = gcode.New(20010, "更新数据失败", "")
	UpdateAppFailed                   = gcode.New(200101, "更新应用失败", "")
	UpdateComponentFailed             = gcode.New(200102, "更新组件失败", "")
	UpdateRegionFailed                = gcode.New(200103, "更新区域失败", "")
	UpdateRoleFailed                  = gcode.New(200104, "更新角色失败", "")
	RoleAssignedByUser                = gcode.New(20012, "角色已分配给用户不能删除,请先清除关联关系", "")
	UserAssignedInRole                = gcode.New(20013, "用户已关联多个角色不能删除,请先清除关联关系", "")
	RoleNotExist                      = gcode.New(20014, "角色不存在", "")
	PermissionNotExist                = gcode.New(20015, "权限不存在", "")
	PermissionAssignedByRole          = gcode.New(20016, "权限已关联多个角色不能更改或删除,请先清除关联关系", "")
	UserNotExist                      = gcode.New(20017, "用户不存在", "")
	AppNotExist                       = gcode.New(20018, "应用不存在", "")
	PermissionAssignedInApplication   = gcode.New(20019, "权限已关联多个应用不能删除,请先清除关联关系", "")
	AppComponentAssignedInApplication = gcode.New(20020, "应用组件已关联该应用不能删除,请先清除关联关系", "")
	RoleIncludingPermission           = gcode.New(20021, "角色已关联多个权限不能删除,请先清除关联关系", "")
	AssignPermissionToRoleFailed      = gcode.New(20022, "将权限分配给角色失败", "")
	UnassignPermissionToRoleFailed    = gcode.New(20023, "将权限从角色中移除失败", "")
	AssignUserToRoleFailed            = gcode.New(20024, "将用户分配给角色失败", "")
	UnassignUserToRoleFailed          = gcode.New(20025, "将用户从角色中移除失败", "")
	MustSelectAtLeastOnePermission    = gcode.New(20026, "至少分配一个权限给角色", "")
	MustSelectAtLeastOneUser          = gcode.New(20028, "至少分配一个用户给角色", "")
	UserAssignedInGroup               = gcode.New(20027, "该用户组包含用户不能删除,请先清除关联关系", "")
	AssignUserToGroupFailed           = gcode.New(20029, "将用户分配给用户组失败", "")
	UnassignUserToGroupFailed         = gcode.New(200230, "将用户从用户组中移除失败", "")
	AtLeastOneUserToGroup             = gcode.New(200229, "至少添加一个用户给用户组", "")
	AtLeastOneUserFromGroup           = gcode.New(200230, "至少从用户组移除一个用户", "")
	ResetPermissionCacheFailed        = gcode.New(20031, "重置权限缓存失败", "")
	IncludeAtLeastOneChild            = gcode.New(20032, "该区域中至少包含一个子区域,不可删除", "")
	TenantNotExist                    = gcode.New(20033, "租户不存在", "")
	TenantIncludeUser                 = gcode.New(20034, "租户下存在用户不可删除", "")
	PasswordOrTenantEmpty             = gcode.New(20034, "登陆失败:密码或租户ID为空", "")
	LoginFailed                       = gcode.New(20035, "登陆失败:用户名或密码错误", "")
	MD5PassFailed                     = gcode.New(20036, "对密码md5加密失败", "")
	MenuHasChildrenNode               = gcode.New(20037, "存在子节点不可删除", "")
	MenuHasChildrenPermission         = gcode.New(20038, "菜单下存在权限定义不可删除", "")
	QueryAppPermissionByMenuFailed    = gcode.New(20039, "通过菜单id查权限失败", "")
	AppMenuParentMustNull             = gcode.New(20040, "应用类型的菜单父菜单必需为空", "")
	AppMenuParentMustNotNull          = gcode.New(20040, "菜单的父菜单不能为空", "")
	AppMenuNotExist                   = gcode.New(20041, "菜单不存在", "")
	QueryLicenseFailed                = gcode.New(200042, "查询许可失败", "")
	LicenseIsActive                   = gcode.New(200043, "许可正在使用当中,不可删除", "")
	LicenseNotExist                   = gcode.New(200044, "许可不存在", "")
	UserGroupNotExist                 = gcode.New(200045, "用户组%d不存在", "")
	AddAppFailed                      = gcode.New(200046, "新增应用失败", "")
	RegionNotExist                    = gcode.New(200047, "新增应用失败", "")
	ListEmployeeFailed                = gcode.New(200048, "查询员工信息失败", "")
	ResetRegionCacheFailed            = gcode.New(200049, "重置区域缓存失败", "")
	GetCachedRegionsFailed            = gcode.New(200050, "读取区域缓存失败", "")
	ParseRegionCacheFailed            = gcode.New(200051, "解析区域缓存失败", "")
	UpdatePermissionFailed            = gcode.New(200052, "更新权限失败", "")
	CodeNotImplemented                = gcode.New(200053, "%d未实现", "")
	UpdateRoleMenuFailed              = gcode.New(200054, "更新角色菜单失败", "")
	QueryRoleMenuFailed               = gcode.New(200055, "查询角色%d菜单失败", "")
	QueryUserRoleFailed               = gcode.New(200056, "根据用户%d查询角色失败", "")
	QueryGroupUserFailed              = gcode.New(200057, "查询用户组%d的用户失败", "")
)

var TokenClaims = &struct {
	Auth          string
	UserId        string
	TenantCode    string
	Username      string
	CompanyCode   string
	PlatformAdmin string
	TenantAdmin   string
}{
	Auth:          "Authorization",
	UserId:        "user_id",
	TenantCode:    "tenant_code",  // 决定用户信息
	Username:      "username",     // 决定用户登陆信息
	CompanyCode:   "company_code", // 决定用户查询数据范畴
	PlatformAdmin: "platform_admin",
	TenantAdmin:   "tenant_admin",
}

// init cache keys
var InitialCacheKeys = &struct {
	RegionsKey     string
	PermissionsKey string
	AppMenusKey    string
	AppsKey        string
	TenantsKey     string
}{
	RegionsKey:     "caas-platform-mc-table-record::regions",
	PermissionsKey: "caas-platform-mc-table-record::permissions",
	AppMenusKey:    "caas-platform-mc-table-record::appmenus",
	AppsKey:        "caas-platform-mc-table-record::applications",
	TenantsKey:     "caas-platform-mc-table-record::tenants",
}

var CacheKeysPrefix = &struct {
	License string
}{
	License: "caas-platform-mc-licenses::tenant:",
}

var PlatformAdminMenus = map[string][]string{
	"000001": {"/tmo/tenants", "/tmo/licenses", "/apm/comps", "/apm/roles", "/apm/apps", "/apm/menus", "/edm/regions"},
}
var TenantAdminMenus = map[string][]string{
	"000001": {"/cam/orgs", "/cam/users", "/cam/groups", "/cam/licenses", "/mdm/employees", "/mdm/orgs"},
}

type TokenKey struct {
	UserId          string `json:"userId"`
	TenantCode      string `json:"tenantCode"`
	Username        string `json:"username"`
	CompanyCode     string `json:"companyCode"`
	IsPlatformAdmin bool   `json:"isPlatformAdmin"`
	IsTenantAdmin   bool   `json:"isTenantAdmin"`
	// AuthType        int    `json:"authType"`
	AppName string `json:"appName"`
}

// var MyAppInfo = AppShortInfo{
// 	Code: "000001",
// 	Name: "mc",
// 	Desc: "管理中心",
// }

// var AuthTypes = map[int]string{
// 	1: "web",
// 	2: "mobile",
// }

// var PermissionTypes = map[int]string{
// 	1: "菜单权限",
// 	2: "接口权限",
// }

// 类型字典
type DdicInfo1 struct {
	Value string `json:"value"`
	Label string `json:"label"`
}

type DdicType1 struct {
	Value int    `json:"value"`
	Label string `json:"label"`
}
type DdicType2 struct {
	Value string `json:"value"`
	Label string `json:"label"`
}

var Genders = [2]DdicType1{
	{Value: 1, Label: "男"},
	{Value: 2, Label: "女"},
}
var RegionLevels = [4]DdicType1{
	{Value: 1, Label: "国"},
	{Value: 2, Label: "省/州"},
	{Value: 3, Label: "地级市"},
	{Value: 4, Label: "区/县"},
}

var OrgTypes = [3]DdicType1{
	{Value: 1, Label: "公司"},
	{Value: 2, Label: "分公司"},
	{Value: 3, Label: "部门"},
}

var UserTypes = [2]DdicType1{
	{Value: 1, Label: "可登录用户"},
	{Value: 2, Label: "不可登陆用户"},
}

var MenuTypes = [4]DdicType1{
	{Value: 1, Label: "应用"},
	{Value: 2, Label: "菜单"},
	{Value: 3, Label: "页面"},
	{Value: 4, Label: "按钮"},
}

var AppTypes = [5]DdicType2{
	{Value: "web", Label: "Web应用"},
	{Value: "weixin", Label: "小程序"},
	{Value: "api", Label: "纯API应用"},
	{Value: "app", Label: "移动app"},
	{Value: "desktop", Label: "桌面客户端"},
}
var CompTypes = [5]DdicType1{
	{Value: 1, Label: "后端API"},
	{Value: 2, Label: "网页端"},
	{Value: 3, Label: "小程序端"},
	{Value: 4, Label: "移动App端"},
	{Value: 5, Label: "桌面客户端"},
}

// 状态字典
type DdicStatus1 struct {
	Value int    `json:"value"`
	Label string `json:"label"`
}
type DdicStatus2 struct {
	Value bool   `json:"value"`
	Label string `json:"label"`
}

var EmployeeStatus = [8]DdicStatus1{
	{Value: 1, Label: "未入职"},
	{Value: 2, Label: "试用期"},
	{Value: 3, Label: "正式员工"},
	{Value: 4, Label: "离职中"},
	{Value: 5, Label: "离职"},
	{Value: 6, Label: "停职"},
	{Value: 7, Label: "退休"},
	{Value: 8, Label: "编外员工"},
}

var LicenseStatus = [4]DdicStatus1{
	{Value: 1, Label: "待审批"},
	{Value: 2, Label: "正常"},
	{Value: 3, Label: "冻结"},
	{Value: 4, Label: "过期"},
}

var TenantStatus = [2]DdicStatus2{
	{Value: false, Label: "禁用"},
	{Value: true, Label: "启用"},
}

var UserStatus = [2]DdicStatus2{
	{Value: false, Label: "禁用"},
	{Value: true, Label: "启用"},
}
